package com.lzb.play.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.play.BaseAc;
import com.lzb.play.Content;
import com.lzb.play.R;
import com.lzb.play.adapter.RecordAdapter;
import com.lzb.play.basic.media.AudioPlayerImpl;
import com.lzb.play.databinding.ActivityRecordBinding;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends BaseAc<ActivityRecordBinding> {

    private List<String> listShow = new ArrayList<>();
    private RecordAdapter recordAdapter;
    private AudioPlayerImpl mAudioPlayer;
    private AudioPlayerImpl mCurAudioPlayer;

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecordBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mAudioPlayer = new AudioPlayerImpl();
        mCurAudioPlayer = new AudioPlayerImpl();

        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext));
        recordAdapter = new RecordAdapter(mAudioPlayer);
        recordAdapter.mCurAudioPlayer = mCurAudioPlayer;
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(new RecordAdapter.OnItemClickListener() {
            @Override
            public void onItemDelete(int position) {
                FileUtils.delete(listShow.get(position));
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() == 0) {
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        } else {
            mDataBinding.tvRecordNoData.setVisibility(View.GONE);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            for (File file : files) {
                listShow.add(file.getPath());
            }
            recordAdapter.setAudioPaths(listShow);
        }
    }

}